﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Input;
using Magnet.Engine.Entities;
using Magnet.Engine;

namespace Magnet.Engine
{
    class ArrowManager
    {
        private static GraphicsDeviceManager mGraphics;

        private static Texture2D arrowTexture;
        private static Vector2[] arrows;
        private static float[] rotations;
        private static Vector2 arrowCenter;
        private static int numArrows;

        private static int width;
        private static int height;

        private static int xOffset;
        private static int yOffset;

        /// <summary>
        /// 
        /// </summary>
        public static void Initialize(GraphicsDeviceManager graphics)
        {
            mGraphics = graphics;

            width = 800;
            height = 600;

            xOffset = 90;
            yOffset = 100;

            InitArrows();
        }

        /// <summary>
        /// 
        /// </summary>
        private static void InitArrows()
        {
            int offset = 20;
            numArrows = 0;

            for (int i = offset + xOffset; i < width + xOffset; i = i + offset)
            {
                for (int j = offset + yOffset; j < height + yOffset; j = j + offset)
                {
                    numArrows++;
                }
            }

            arrows = new Vector2[numArrows];
            rotations = new float[numArrows];

            int k = 0;

            for (int i = offset + xOffset; i < width + xOffset; i = i + offset)
            {
                for (int j = offset + yOffset; j < height + yOffset; j = j + offset)
                {
                    arrows[k].X = i;
                    arrows[k].Y = j;

                    rotations[k] = 0f;

                    k++;
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="content"></param>
        public static void LoadContent(ContentManager content)
        {
            // TODO: Load any ResourceManagementMode.Automatic content
            // arrowTexture = content.Load<Texture2D>("compass_arrow");

            arrowTexture = content.Load<Texture2D>("compass_arrow");

            arrowCenter = new Vector2(arrowTexture.Width / 2f, arrowTexture.Height / 2f);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="elapsedSeconds"></param>
        public static void Update(float elapsedSeconds)
        {
            updateArrows();
        }

        /// <summary>
        /// 
        /// </summary>
        private static void updateArrows()
        {
            Vector2 arrowPosition;
            Vector2 arrowVector;

            int length = Magnet.Engine.World.GetEntityCount();
            Entities.Magnet m;

            for (int i = 0; i < numArrows; i++)
            {
                arrowPosition = arrows[i];
                arrowVector = Vector2.Zero;

                for (int j = 0; j < length; j++)
                {
                    m = Magnet.Engine.World.GetEntityAt(j) as Entities.Magnet;

                    if (m != null && !(m is Player))
                    {
                        arrowVector += m.GetForceAtPosition(arrowPosition);
                    }
                }                

                if (arrowVector != Vector2.Zero)
                {
                    arrowVector.Normalize();
                    rotations[i] = (float)Math.Atan2((double)arrowVector.X, (double)-arrowVector.Y);
                }
                else
                    rotations[i] = 0f;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="spriteBatch"></param>
        public static void Draw(SpriteBatch spriteBatch)
        {
            Color color = new Color(Color.White.R, Color.White.G, Color.White.B, 100);

            for (int i = 0; i < numArrows; i++)
            {
                spriteBatch.Draw(arrowTexture, arrows[i], null, color, rotations[i],
                    arrowCenter, 1f, SpriteEffects.None, 0f);
            }
        }
    }
}
